using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using ProjectEuler.Core.Helpers;

namespace ProjectEuler.Core
{
    public class Problem39 : IProjectEulerProblem
    {
        protected IDictionary<int, int> RightTranglesPerPerimeter()
        {
            var dict = new Dictionary<int, int>();
            for (int p = 3; p <= 1000; p++)
            {
                dict.Add(p, RightTrianglesWithPerimeterOf(p).Count());
            }
            return dict;
        }

        protected IEnumerable<Triplet> RightTrianglesWithPerimeterOf(int p)
        {
            for(int a=1;a<=(p-2);a++)
            {
                for(int b=1;b<=(p-a-1);b++)
                {
                    if (Triplet.IsTripletPythagorean(a, b, p - a - b))
                    {
                        yield return new Triplet(a, b, p - a - b);
                    }
                }
            }
        }

        public int Number
        {
            get { return 39; }
        }

        public string Description
        {
            get { return "For which value of p <= 1000, is the number of solutions maximised?"; }
        }

        public string Answer
        {
            get { return RightTranglesPerPerimeter().OrderByDescending(kvp => kvp.Value).First().Key.ToString(); }
        }
    }

    [TestFixture]
    public class Problem39Tests: Problem39
    {
        [Test]
        public void There_are_exactly_three_solutions_for_p_of_120()
        {
            var triangles = RightTrianglesWithPerimeterOf(120);
            Assert.AreEqual(3, triangles.Count());
        }
    }
}